﻿Shader "Running/Building"
{
	Properties
	{
		_ambientLightColor("_ambientLightColor", Color) = (1, 1, 1, 1)
		_shadowMap("shadowMap", 2D) = "white" {}

		inverseShadowmapSize("inverseShadowmapSize", Float) = 1
		fixedDepthBias("fixedDepthBias", Float) = 0
		gradientClamp("gradientClamp", Float) = 0
		gradientScaleBias("gradientScaleBias", Float) = 1

		shadowColor("shadowColor", Color) = (0, 0, 0, 1)

	}
	SubShader
	{
		Tags { 
			"RenderType"="Opaque" 
			"ShadowRender" = "Build"
		}

		LOD 100

		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			// make fog work
			#pragma multi_compile_fog
			
			#include "UnityCG.cginc"
			#include "Lighting.cginc"
			#include "tools.cginc"

			struct appdata
			{
				float4 vertex : POSITION;
				float4 normal : NORMAL;
				float4 uv : TEXCOORD0;
				float4 color : COLOR;
			};

			struct v2f
			{
				float4 vertex : SV_POSITION;
				float4 color : COLOR;
				float4 shadowUV : TEXCOORD0;
				float4 diffuseLightColor : TEXCOORD1;
				float4 decal1UV : TEXCOORD2;
			};

			
			v2f vert (appdata v)
			{
				v2f o;
				float4 worldPos = mul(unity_ObjectToWorld, v.vertex);
				o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
				o.decal1UV = v.uv;
				o.color = v.color;
				o.diffuseLightColor = lightning(_WorldSpaceLightPos0, _LightColor0, worldPos, unity_WorldToObject, v.normal);
				o.shadowUV = mul(texViewProj, worldPos);
				return o;
			}
			
			uniform float4 _ambientLightColor;
			sampler2D _shadowMap;
			uniform float fixedDepthBias;
			uniform float gradientClamp;
			uniform float gradientScaleBias;
			uniform float4 shadowColor;

			fixed4 frag(v2f IN) : SV_Target
			{
				// sample the texture
				float4 result = IN.color * _ambientLightColor +
					IN.color * IN.diffuseLightColor *
				inShadow(IN.vertex, IN.shadowUV, _shadowMap, inverseShadowmapSize, fixedDepthBias, gradientClamp, gradientScaleBias, shadowColor.a);

				return result;
			}
			ENDCG
		}
	}
}
